<?php
	/**
	 * 后台管理分类控制器
	 */
	 class CategoryController extends AuthController{
		
	 	/**
		 *  显示所有的顶级分类
		 */
		public function index(){
			$model = M('category');
			$result = $model->query('SELECT * FROM hd_category WHERE pid=0');
			$this->assign('result',$result);
			$this->display();
		}
		
		/**
		 * 修改当前分类的名字
		 */
		 public function editcate(){
		 	$model = M('category');
		 	if(IS_POST){
		 		$cid = $_POST['cid'];
				$title = $_POST['title']; 
				$result = $model->_exec("UPDATE hd_category SET title='{$title}' WHERE cid={$cid}");
				if($result){
					$this->success('分类修改成功',__APP__.'?c=category');
				}
		 	}
		 	$cid = $_GET['cid'];
			$result = current($model->query("SELECT * FROM hd_category WHERE cid={$cid}"));
			$this->assign('result',$result);
			$this->display();			
		 }
		 
		 /**
		  * 添加子分类
		  */
		  public function addcate(){
		  		
		  	$model = M('category');
		 	if(IS_POST){
				$title = $_POST['title']; 
				$pid = $_POST['pid'];
				$result = $model->_exec("INSERT INTO  hd_category(title,pid) VALUES('{$title}',{$pid})");
				if($result){
					$this->success('添加子分类成功',__APP__.'?c=category');
				}
		 	}
		 	$pid = $_GET['pid'];
			$this->assign('pid',$pid);
			$this->display();	
		  }
		  
		  /**
		   * 添加顶级分类
		   */
		  public function addtopcate(){
		  		$model = M('category');
			 	if(IS_POST){
					$title = $_POST['title']; 
					$result = $model->_exec("INSERT INTO  hd_category(title,pid) VALUES('{$title}',0)");
					if($result){
						$this->success('添加顶级分类成功',__APP__.'?c=category');
					}
			 	}
				$this->display();	
		  }	
		  
		  /**
		   * ajax请求舍查看当前分类下面的子类
		   */
		 public function ajaxcate(){
		 	$cid = $_POST['cid'];
			$result = $model = M('category')->query("SELECT cid,title,pid FROM hd_category WHERE pid={$cid}");
			if(!$result){
				echo 0;
			}else{
				echo json_encode($result);	
			}
		 }
		 
		 	/**
			 * 删除一个分类
			 * 如果一个分类下面还存在子分类那么也要一起删除他
			 */
			 public function del(){
			 	$cid = $_GET['cid'];
			 	//先将所有的数据遍历出来
			 	$cates = M('cate')->query("SELECT * FROM hd_category");
				$cate = $this->isdel($cates,$cid);
				//将他们组为一个in (1,2,43,4)再删除
				//组合一下当前父类
				$isdelcate = $this->delcates($cate,$cid);
								
			 }
			 
			 private function string($cate,$type,$cid=null){
			 	if($type=='cid'){
			 		$str ='' ;
					foreach($cate as $c){
						$str .= $c['cid'].',';					
					}
					$str = rtrim($str,',');
					$str = $cid.','.$str;	
			 	}else if($type=='asid'){
			 		$str ='' ;
					foreach($cate as $c){
						$str .= $c['asid'].',';					
					}
					$str = rtrim($str,',');
					$str = $cid.','.$str;
			 	}else if($type=='anid'){
			 		$str ='' ;
					foreach($cate as $c){
						$str .= $c['anid'].',';					
					}
					if(!is_null($cid)){
						$str = rtrim($str,',');
						$str = $cid.','.$str;	
					}else{
						$str = ltrim($str,',');
					}
					
				}
				return $str;
			 }
			 
			 
			 private function delcates($cate,$cid){
				
				
				$cate = $this->string($cate,'cid',$cid);
				//获得所有要被删除的提问的数据拿到他们的asid再去获得他们相应的回答 再删除即可
				$asks = M('asks')->query("SELECT * FROM hd_ask WHERE cid IN({$cate})");
				//获得所有 相关分类 下面的所有提问
				$asids = ltrim($this->string($asks,'asid'),',');
				//获得所有相关提问下面所有的回答
				$answers = M('asks')->query("SELECT * FROM hd_answer WHERE asid IN({$asids})");
				$anids = rtrim($this->string($answers,'anid'),',');
				//删除所有的回答
				M('category')->_exec('DELETE FROM hd_answer WHERE anid IN('.$anids.')');
				//删除所有的提问
				M('category')->_exec('DELETE FROM hd_ask WHERE asid IN('.$asids.')');
				//删除相应分类下边的所有分类
				M('category')->_exec('DELETE FROM hd_category WHERE cid IN('.$cate.')');	
				$this->success('删除分类成功',__APP__.'?c=category');
				
			 }
			 
		 	/**
			 * 递归查找当前分类下面的子分类
			 */
			 private function isdel($data,$cid){
			 	$arr = array();
			 	foreach($data as $d){
			 		//如果pid等于传入的cid那么就将这条数据压入数组
			 		if($d['pid']==$cid){
			 			$arr[] = $d;
			 		}
				}
				foreach($arr as $a){
					$arr = array_merge($arr,$this->isdel($data,$a['cid']));	
				}
				return $arr;
			 }
			 
	 }
	